//查询
goodsDistributionMode()
function goodsDistributionMode() {
    $.ajax({
        url:root.goodsInter.getSkuLogisticeLimitList,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "nowPage":1,
            "pageSize":30,
            "model": {
                "ableLogisticeType":$("#ableLogisticeType").val(),
                "skuCode":$("#skuCode").val(),
                "spuCode":$("#spuCode").val(),
                "unableLogisticeType":$("#unableLogisticeType").val(),
            }
        }),
        success: function (data) {
            // couponlist(data.result);
            couponlist(data.data);
            $('.M-box3').pagination({
                pageCount: data.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.goodsInter.getSkuLogisticeLimitList,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageSize": "30",
                            "nowPage": api.getCurrent(),
                            "model": {
                                "ableLogisticeType":$("#ableLogisticeType").val(),
                                "skuCode":$("#skuCode").val(),
                                "spuCode":$("#spuCode").val(),
                                "unableLogisticeType":$("#unableLogisticeType").val(),
                            }
                        }),
                        success: function (result) {
                            couponlist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function couponlist(data, index){

    // var j = 0;
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组


    if(data !=null && data != ''){
        var datashu =data.startRow+1;
        console.log(data.data)
        $.each(data.data, function (i, field) {
            var datashus = datashu++
            var rowData = new Array();
            rowData.push(datashus);
            rowData.push(field.skuCode);
            rowData.push(field.spuCode);
            rowData.push(field.unableLogistice);
            rowData.push(field.ableLogistice);
            rowData.push(field.restricted);
            rowData.push(field.createTime);
            rowData.push(field.createBy);
            dataSource.push(rowData);

        });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                       var val =""
                        if (row[5] == 3) {
                            val = '否'
                        }else if (row[5] == 2) {
                            val = '是'
                        }else if (row[5] == 1) {
                            val = '是'
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[6]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '<a onclick="removes(\'' + row[1] + '\')">移除</a>'

                        return val;
                    },
                    "targets": 8
                },


            ]
        });
//权限控制函数
        power()
    };
}

//导入
function upLoadFile() {
    $("#fileUp").trigger('click');
}

function Imports() {
    var name=$("#fileUp").val()
    var result = /\.[^\.]+/.exec(name);
    if (result != ".xlsx" && result != ".xls") {
        $.Huimodalalert('格式不正确！',2000);
        return false;
    }
    var formData = new FormData();
    formData.append('file', $("#fileUp")[0].files[0]);
    formData.append('creationBy', JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID);
    formData.append('creationName', JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name);
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/aproduct/import/product/skuShip',
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function (data) {
            console.log(data)
            $('#fileUp').val('');
            if(data.errorCode==0){
                $.Huimodalalert('导入成功！',2000);
                goodsDistributionMode()

            }else {
                $.Huimodalalert(data.msg,2000);
            }

        }
    });
}

//添加
var index=0;
function addgoodsDistributionMode() {
    var cu = layer.confirm('<span class="c-red pr-5 f-l">*</span>code码：<input id="skuCodes" autocomplete="off" type="text" class="input-text radius" style="width: 141px;" placeholder="请输入code码"><br><span class="c-red pr-5 f-l">*</span>不可用物流：<br><span><input type="checkbox" name="customerType" value="1">鑫方盛物流</span> <span><input type="checkbox" name="customerType" value="2">快递</span>  <span><input type="checkbox" name="customerType" value="3">快运</span>  <span><input type="checkbox" name="customerType" value="4">专车包车</span>', {title:'添加设置',btn: ['确定', '取消']}, function(index){
        addgoodsDistributionMode2(cu)

    },function(){
        layer.close(cu);


    });

}
//第二个
function addgoodsDistributionMode2(cu){
    var customerType = $("input:checkbox[name='customerType']:checked").map(function(index,elem) {
        return $(elem).val();
    }).get().join('、');

    if($("#skuCodes").val()==""){
        layer.msg('请填写code码！', {icon: 5,time:1000});
        return false;
    }
    if(customerType==""){
        layer.msg('请选择配送方式！', {icon: 5,time:1000});
        return false;
    }
        $.ajax({
            url: root.goodsInter.addSkuLogisticeLimit,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            contentType: "application/json",
            data: JSON.stringify({
                "skuCode":$("#skuCodes").val(),
                "creationName": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
                "unableLogistice":customerType,
            }),
            success: function (data) {
                    if(data.errorCode==0){
                        $.Huimodalalert('添加成功！',2000);
                        layer.close(cu);
                        goodsDistributionMode()
                    }else {
                        layer.msg(data.msg, {icon: 5,time:1000});
                    }
            },
            error: function () {
                layer.msg('失败！', {icon: 5,time:1000});
            }
        })

}
//移除
function removes(id) {
    var curInd = layer.confirm(
        '<span>确定移除吗？</span>'+'<br/>', {title:'提示',btn: ['确定', '取消']}, function(index){
            $.ajax({
                url:root.goodsInter.delSkuLogisticeLimit,
                cache: false,  //禁用缓存
                type: "POST",
                contentType: "application/json",
                dataType:'json',
                data: JSON.stringify({
                    "skuCode":id,

                }),
                success: function (data) {
                    if(data.errorCode==0){
                        $.Huimodalalert("移除成功",2000);
                        goodsDistributionMode()

                    }else {
                        $.Huimodalalert(data.msg,2000);
                    }
                },
                error: function () {
                    $.Huimodalalert('失败！',2000);
                }
            });
            layer.close(curInd);

        },function(){
            layer.close(curInd);

        });

}